<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

/**
 * App\Models\ShopCategory
 *
 * @property int $id
 * @property string $name 名称
 * @property int $sort 排序
 * @property int $status 状态:0=正常,1=隐藏
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|BusinessCategory newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|BusinessCategory newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|BusinessCategory query()
 * @method static \Illuminate\Database\Eloquent\Builder|BusinessCategory whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|BusinessCategory whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|BusinessCategory whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|BusinessCategory whereSort($value)
 * @method static \Illuminate\Database\Eloquent\Builder|BusinessCategory whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|BusinessCategory whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class BusinessCategory extends Model
{
	use HasDateTimeFormatter;
    protected $table = 'business_categorys';

    protected static function booted()
    {
        self::deleting(function (BusinessCategory $category){
//            $exit = Coupon::query()->where("cid",$category->id)->exists();
//            if ($exit){
//                modelError("此分类关联优惠券信息,不可删除!");
//            }
        });
    }

    public static function getOptions(): Collection
    {
        return self::query()->orderByDesc("sort")->orderByDesc("id")->pluck("name","id");
    }

    public static function getList(): Collection|array
    {
        return self::query()->where("status",0)->orderByDesc("sort")->orderByDesc("id")->get(['id','name']);
    }


}
